using Game.Main.Model;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class RideItem : BaseItem
	{
		private CharacterItem rideCharacter;

		private Transform leftTyre;

		private Transform rightTyre;

		private float angle;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			leftTyre = model.Find("LeftTyre");
			rightTyre = model.Find("RightTyre");
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (isDrag)
			{
				angle -= Time.deltaTime * 180f;
				leftTyre.SetLocalRotationZ(angle);
				rightTyre.SetLocalRotationZ(angle);
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			bool flag = base.AttachItem(item, depthType, attachType, updatePosition);
			if (flag && rideCharacter == null && item.itemData.scriptType == 1)
			{
				rideCharacter = (CharacterItem)item;
				CheckRideCharacter();
			}
			return flag;
		}

		public override void ReleaseAttachItem(BaseItem item)
		{
			base.ReleaseAttachItem(item);
			if (item == rideCharacter)
			{
				rideCharacter.SetHalfSideToBack(false, false);
				rideCharacter = null;
			}
		}

		public override void FlipHorizontal(bool turnToFront)
		{
			base.FlipHorizontal(turnToFront);
			CheckRideCharacter();
		}

		private void CheckRideCharacter()
		{
			if (rideCharacter != null)
			{
				if (archiveData.dirX == -1)
				{
					rideCharacter.SetHalfSideToBack(true, true);
				}
				else
				{
					rideCharacter.SetHalfSideToBack(true, false);
				}
			}
		}
	}
}
